from sqlalchemy import create_engine
from sqlalchemy.orm import Session,sessionmaker, scoped_session
from contextlib import contextmanager

db_engine = create_engine('mysql+pymysql://root:root@127.0.0.1/car_db', echo=False, pool_size=1000)


# 使用上下文管理器创建会话
@contextmanager
def get_db_session():
    Session = scoped_session(sessionmaker(bind=db_engine))
    try:
        db_session = Session()
        yield db_session
        db_session.commit()
    except Exception as e:
        db_session.rollback()
        raise e
    finally:
        db_session.close()




